package com.tj.ocr.similar;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.imageio.ImageIO;

import com.tj.ocr.img.BlankWhiteUtil;
import com.tj.ocr.img.ImageRW;
import com.tj.ocr.img.LinePop;
import com.tj.ocr.img.MyImage;
import com.tj.ocr.img.TextPop;

public class TextMatcher {

	private  static final int line_max = 1000;
	private static List<Integer> winSizeList;
	private static Map<Integer,ImageIndex> fingerPrintList;

	//TODO: read from config file
	public static void init(String dataPath, int startLeftPos){
		try {
			System.out.println("init textMatch begin! This process may take a few minutes");
			
			TextPop.start_pos = startLeftPos;
			
			winSizeList = new ArrayList<>();
			fingerPrintList = new HashMap<>();
			
			ImageIndex fingers = null;
			Map<Integer,Integer> sizeCount = new HashMap<>();
			
			File dir = new File(dataPath);
			if(dir.isDirectory()){
				File[] fs = dir.listFiles();
				//build fingerPrint
				for(File file : fs){
					BufferedImage bi = ImageIO.read(file);
					
					int width = bi.getWidth();
					if(fingerPrintList.containsKey(width)){
						sizeCount.put(width, sizeCount.get(width)+1);
						fingers = fingerPrintList.get(width);
					}else{
						sizeCount.put(width,1);
						//winSizeList.add(width);
						fingers = new ImageIndex();
						fingerPrintList.put(width, fingers);
					}

					String fname = file.getName();
					if(fname.contains("暂")){
						fname = fname + "";
					}
					FingerPrint fp = FingerPrint.get(bi);
					fname = fname.substring(0, fname.length()-4);
					fp.setText(fname);

					fingers.add(fp, bi.getHeight());
				}
				
				//sort size
				sortSize(sizeCount);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		

		for(Integer mi : fingerPrintList.keySet()){
			System.out.println(mi);
			System.out.println(fingerPrintList.get(mi).toString());
		}
		
		System.out.println("init textMatch complete!");
		/*List<FingerPrint> lfp = new ArrayList<>();
		data.put(1D, lfp);*/
	}
	
	private static void sortSize(Map<Integer, Integer> sizeCount) {
		List<Integer> ls = new ArrayList<>();
		
		for(Integer count : sizeCount.values()){
			ls.add(count);
		}
		
		Collections.reverse(ls);
		for(int lz : ls){
			Integer si = null;
			for(Integer key : sizeCount.keySet()){
				if(sizeCount.get(key) == lz){
					winSizeList.add(key);
					si = key;
					break;
				}
			}
			
			sizeCount.remove(si);
		}
		
		
	}

	public static String doTextSearch(MyImage bi) {
		String ret = "";
		int start_pos = TextPop.start_pos;
		int pos = start_pos + bi.getFromx();
		int fromy = bi.getFromy();
		int width = bi.getWidth(), height = bi.getHeight();
		int ci = 0;
		while(ci ++ < line_max){
			boolean hasData = false;
			//slip one by one
			for(int w : winSizeList){
				if(pos + w >= width) return ret;
				
				//slip image
				MyImage subi = TextPop.subImage(bi, pos, fromy, w, height);
				//ImageRW.write(subi.realImage(), "d:\\test\\c1\\r"+w+ci+".png");
				//get fingerPrint
				FingerPrint fp = FingerPrint.getEx(subi,subi.getHeight());
				FingerPrint rfp = null;
				ImageIndex fps = fingerPrintList.get(w);
				if(fps != null && (rfp = fps.get(fp, height)) != null){
					//check fingerPrint
					ret += rfp.getText();
					pos += w;
					hasData = true;
					break;
				}
			}
			
			if(hasData == false)
				pos ++;
		}
		
		return ret;
	}
	
	public static void main(String[] args){
		init("d:\\test\\wxc",4);
		
		//get text
		BufferedImage bi = ImageRW.read("d:\\test\\tt.png");
		MyImage mi = BlankWhiteUtil.procEx(bi, 170);
		
		LinePop lpop = new LinePop(mi);
		while((mi = lpop.pop()) != null){
			System.out.println(doTextSearch(mi));
		}
		
		/*//test
		TextPop pop = new TextPop(bi);
		BufferedImage bi = null;
		while((bi = pop.pop()) != null){
			if(bi != null){
				FingerPrint f = FingerPrint.get(bi);
				//System.out.print(f.getId() + " ");
				List<FingerPrint> rr = data.get(f.getId());
				if(rr != null){
					FingerPrint r = rr.get(0);
					System.out.println(r.getText());
				}
				
			}
				
			//System.out.println(1);
		}*/
	}
}
